package org.jeecg.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * 优雅的http请求方式RestTemplate
 *
 * @author: jeecg-boot
 * @Return:
 */
@Configuration
public class RestTemplateConfig {
    public final static Integer DEFAULT_READ_TIME_OUT = 60000;

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        //修改默认String转换器  默认为 charesets =  StandardCharsets.ISO_8859_1;
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //ms毫秒
        factory.setReadTimeout(DEFAULT_READ_TIME_OUT);
        //ms毫秒
        factory.setConnectTimeout(3000);
        return factory;
    }


}
